package cn.beforedark.yygh.userinfo.controller;


import cn.beforedark.yygh.common.result.R;
import cn.beforedark.yygh.common.util.JwtHelper;
import cn.beforedark.yygh.userinfo.service.UserInfoService;
import cn.beforedark.yygh.dto.user.UserAuthDto;
import cn.beforedark.yygh.dto.user.LoginDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * /user/userinfo/account
 */
@RestController
@RequestMapping("/user/userinfo/account")
public class AccountController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 用户手机验证码登录
     *
     * @param loginDto loginDto
     * @return vo
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginDto loginDto) {
        Map<String, Object> map = userInfoService.login(loginDto);
        return R.ok().data(map);
    }

    /**
     * 获取用户信息: 请求头需要 token
     *
     * @return vo
     */
    @GetMapping("/detail")
    public R detail() {
        // 封装可以拿到 token
        Long id = userInfoService.getThisUserId();
        Map<String, Object> userInfo = userInfoService.detail(id);
        return R.ok().data(userInfo);
    }

    /**
     * 更新实名信息
     *
     * @param token       token
     * @param userAuthDto userAuthDto
     * @return vo
     */
    @PostMapping("/update")
    public R updateUserAuth(@RequestHeader String token, @RequestBody UserAuthDto userAuthDto) {
        Long userId = JwtHelper.getUserId(token);
        userInfoService.updateUserAuth(userId, userAuthDto);
        return R.ok();
    }
}
